<template>
	<view>
		<!-- header start-->
		<view class="header">
			<view class="header__bg"></view>
			<view class="header__con">
				<view class="user-info">
					<view class="info-content">
						<view class="nickname">{{userInfo.name || '未登录'}}</view>
						<view class="phone" v-if="userInfo.phone">{{formatPhone(userInfo.phone)}}</view>
					</view>
					<view class="avatar-box" @tap="handleLogin">
						<image class="avatar"
							:src="userInfo.avatar || 'https://ts2.cn.mm.bing.net/th?id=OIP-C.uwWLUS0SeweiTbLzvWqYYQAAAA&w=250&h=250&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2'"
							mode="aspectFill">
						</image>
					</view>
				</view>
			</view>
		</view>
		<!-- header end -->

		<!-- content start-->
		<view class="content">我的服务</view>
		<view class="grid-box">
			<u-grid :col="4" :border="false">
				<u-grid-item>
					<u-icon name="/static/my/icon-1.png" :size="50"></u-icon>
					<view class="grid-box__text">积分签到</view>
				</u-grid-item>
				<u-grid-item>
					<u-icon name="/static/my/icon-2.png" :size="50"></u-icon>
					<view class="grid-box__text">积分商城</view>
				</u-grid-item>
				<u-grid-item>
					<u-icon name="/static/my/icon-3.png" :size="50"></u-icon>
					<view class="grid-box__text">领劵中心</view>
				</u-grid-item>
				<u-grid-item @tap="contactUs">
					<u-icon name="/static/my/icon-4.png" :size="50"></u-icon>
					<view class="grid-box__text">联系客服</view>
				</u-grid-item>
			</u-grid>
		</view>
		<!-- content end -->

		<!-- cell start -->
		<view class="u-m-l-30 u-m-r-30">
			<u-cell-group>
				<u-cell-item icon="order" @click="gotoOrder()" title="我的订单"></u-cell-item>
				<u-cell-item icon="coupon" @click="gotoCoupon()" title="我的优惠劵"></u-cell-item>
				<u-cell-item icon="setting" @click="gotoSettings()" title="设置"></u-cell-item>
				<u-cell-item icon="star" @click="aboutUs()" title="关于我们"></u-cell-item>
			</u-cell-group>
		</view>
		<!-- cell end -->

		<!-- 普通弹窗 -->
		<uni-popup ref="popup" background-color="#fff" @change="change">
			<view class="popup-content">
				<image class="wechat-icon" src="@/static/login/wechat.png" mode="aspectFit"></image>
				<button class="login-button" @click="wxLogin">微信一键登录</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getUserDetail,
		getLogin,
	} from '@/request/API.js'

	export default {
		data() {
			return {
				userInfo: {},
				type: "bottom",
				showPopup: false, // 控制弹出框显示状态
			};
		},
		onShow() {
			this.getUserInfo();
			// 监听事件
			uni.$on('triggerLoginPopup', this.handleLogin);
		},
		beforeDestroy() {
			// 组件销毁前移除事件监听
			uni.$off('triggerLoginPopup', this.handleLogin);
		},
		methods: {
			// 获取用户信息
			async getUserInfo() {
				try {
					const {
						data
					} = await getUserDetail()
					this.userInfo = data
					// 更新本地存储
					uni.setStorageSync('userInfo', JSON.stringify(data))
				} catch (error) {
					console.log('获取用户信息失败', error)
				}
			},
			// 格式化手机号
			formatPhone(phone) {
				if (!phone) return '';
				return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
			},
			// 处理登录点击
			handleLogin() {
				if (!this.userInfo.name) {
					// 未登录时跳转到登录页
					this.$refs.popup.open('bottom');
				}
			},
			wxLogin() {
				let that = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: async ({
						code
					}) => {
						const {
							data
						} = await getLogin(code);
						uni.setStorageSync("token", data.token);
						that.$refs.popup.close();
						that.getUserInfo();
					}
				})
			},
			// 跳转到订单页
			gotoOrder() {
				uni.switchTab({
					url: '/pages/order/order'
				})
			},
			// 跳转到优惠券页
			gotoCoupon() {
				uni.navigateTo({
					url: '/pages/coupon/coupon'
				})
			},
			// 跳转到设置页
			gotoSettings() {
				uni.navigateTo({
					url: '/pages/settings/settings'
				})
			},
			// 关于我们
			aboutUs() {
				uni.showModal({
					title: '关于我们',
					content: '私房菜餐厅订餐系统 v1.0.0\n感谢您的使用！',
					showCancel: false
				})
			},
			// 联系客服
			contactUs() {
				uni.makePhoneCall({
					phoneNumber: '400-xxx-xxxx'
				})
			},
			change(e) {
				// console.log('当前模式：' + e.type + ',状态：' + e.show);
			},
		}
	};
</script>

<style lang="scss">
	/* 样式 */
	.container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 100vh;
	}

	.open-modal-button {
		padding: 10px 20px;
		font-size: 16px;
		background-color: #1aad19;
		color: white;
		border-radius: 5px;
		margin-bottom: 20px;
	}

	.popup-content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width: 100%;
		padding: 20px;
		background-color: white;
		border-top-left-radius: 10px;
		border-top-right-radius: 10px;
	}

	.wechat-icon {
		width: 80px;
		height: 80px;
		margin-bottom: 20px;
	}

	.login-button {
		width: 80%;
		max-width: 300px;
		background-color: #1aad19;
		color: white;
		font-size: 16px;
		border-radius: 50px;
		padding: 12px 0;
	}

	/* 微信小程序 */
	/* #ifdef MP-WEIXIN */
	.login-button {
		background-color: #57c41e;
	}

	/* #endif */

	.header {
		position: relative;
		height: 480rpx;

		&__bg {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			height: 480rpx;
			background: linear-gradient(180deg, #EE2F37 0%, #f5f5f5 100%);
			z-index: 1;
		}

		&__con {
			position: relative;
			z-index: 2;
			padding: 120rpx 40rpx 0;

			.user-info {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 60rpx;

				.info-content {
					.nickname {
						font-size: 40rpx;
						color: #fff;
						font-weight: bold;
						margin-bottom: 16rpx;
					}

					.phone {
						font-size: 28rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}

				.avatar-box {
					.avatar {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						border: 4rpx solid #fff;
					}
				}
			}
		}
	}

	.content {
		font-weight: bold;
		font-size: 30rpx;
		margin: -180rpx 30rpx 30rpx;
		color: #333;
		position: relative;
		z-index: 2;
		margin-top: 60rpx;
	}

	.grid-box {
		margin: 0 30rpx;
		background: #fff;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		border-radius: 16rpx;
		position: relative;
		z-index: 2;

		&__text {
			margin-top: 20rpx;
			font-size: 24rpx;
			font-weight: bold;
			color: #333;
		}
	}

	/* 设置项 */
	.u-cell-group {
		margin-top: 30rpx;
		background: #fff;
		border-radius: 16rpx;
		overflow: hidden;
	}

	.u-cell-item {
		font-size: 28rpx;
		color: #333;
	}
</style>